import DrawBaseItem from './DrawBaseItem';
import {Polygon} from 'pixi.js';

/**
 * 多边形
 *
 * PolygonItem
 * 
 * @extends DrawBaseItem
 * @class
 * @memberof Nice.DrawBase
 * @author iwangcj
 */
export default class PolygonItem extends DrawBaseItem 
{
    constructor(path) {
        super();

        /**
         * @member {Polygon}
         * @default 
         */
        this.polygon = this.polygon(path);

    }

    polygon(path) {
        let points = path;

        let closeStroke = true;// !!this._fillStyle;

        // check if data has points..
        if (points.points)
        {
            closeStroke = points.closeStroke;
            points = points.points;
        }

        if (!Array.isArray(points))
        {
            // prevents an argument leak deopt
            // see section 3.2: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments
            points = new Array(arguments.length);

            for (let i = 0; i < points.length; ++i)
            {
                points[i] = arguments[i]; // eslint-disable-line prefer-rest-params
            }
        }

        const shape = new Polygon(points);

        shape.closeStroke = closeStroke;
        return shape;
    }

    draw() {
        this.drawShape(this.polygon);
    }

    /**
     * get/set 属性
     * @param {*} key 
     * @param {*} value 
     */
    attr(key, value) {
        if (undefined != value) {
            let isOk = this.attrBase(key, value);
            !isOk && (isOk = this.attrStyle(key, value)); 

            if (isOk) {
                this.draw();
            }
        }
        else {
            let result = this.attrBase(key);
            undefined == result && (result = this.attrStyle(key));
            return result;
        }
    }
}